
// tslint:disable 蜂喔商户端-》消息-》
import { Component, Input, OnInit } from '@angular/core';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { ChatService } from 'src/app/shared/service/chatService';
import { EventService } from 'src/app/shared/service/event.service';
import { HttpService } from 'src/app/shared/service/http.service';
import { IndexedDBService } from 'src/app/shared/service/indexed-db.service';
import { PageModalService } from 'src/app/shared/service/page-modal-service';

@Component({
  selector: 'app-feng-wo-chat',
  templateUrl: './feng-wo-chat.component.html',
  styleUrls: ['./feng-wo-chat.component.scss'],
})
export class FengWoChatComponent implements OnInit {

  @Input() tagData: any;
  sessionId = localStorage.getItem("sessionId");
  currentJid = localStorage.getItem("userId"); // 聊天使用的是用户Id
  msgDatalist = [];
  ifLogin: any;
  isShowTag = false;
  isHasNewMsg = false;
  tempMsgDataList = [];
  pageNo: any = 0;
  pageSize: any = 20;
  title = "好友列表";
  rightLabel = "更多";
  constructor(
    private appGlobalService: AppGlobalService,
    private pageModalService: PageModalService,
    public chatService: ChatService,
    private http: HttpService,
    private eventService: EventService
  ) {
  }

  ngOnInit() {
    if (localStorage.getItem("loginStatus")) {
      this.isShowTag = true;
      this.initGroupList(() => {
        this.appGlobalService.consoleLog('initMyFriendList', '  	   ');
      });
    }
  }

  openModal(item) {
    const that = this;
    const targetUserId = item.friendNo;
    // tslint:disable-next-line:no-string-literal
    item['groupNo'] = targetUserId;
    if (item.groupNo) {
      that.chatService.enterTheChatRoomUseUserId = item.groupNo;
      that.chatService.currentGroupName = item.friendName ? item.friendName : item.groupName;
    }
    localStorage.setItem('enterChatRoomByUserId', '1');
    that.eventService.eventEmit.emit('enterChatRoomByUserId',
      {
        userId: item.groupNo,
        fromFriendList: true,
        friendName: item.friendName,
        friendAvatar: item.friendAvatar
      }
    );
  }


  // 2023.12.01 切换数据源
  initGroupList(callbackFunction) {
    const userId = localStorage.getItem('userId');
    const url = this.http.talkQueryFriendsList + this.chatService.getPaginationParamStr(userId, this.pageSize, this.pageNo);
    const tempMsgDataList = [];
    this.pageModalService.getGetData(url, (response) => {
      if (response) {
        const data = JSON.parse(response);
        // this.appGlobalService.consoleLog('查询到数据', '');
        if (data
          && data.listData !== undefined
          && data.listData.length > 0
        ) {
          // tslint:disable-next-line:prefer-for-of
          for (let index = 0; index < data.listData.length; index++) {
            const element = data.listData[index];
            if (element.hasNoRead) {
              this.chatService.unreadFriendList.add(element.friendNo);
            }
          }
          setTimeout(() => {
            this.chatService.setMyFriendData(data);
          }, 200);

          // this.pageModalService.chatService.setMyFriendData(data);
        }
      }
    });
  }


  goPage(obj) {
    try {
      if (obj) {// obj
        if (this.isHasNewMsg) {
          this.isHasNewMsg = false;
          if (this.appGlobalService.clientType === 'js') {
            this.eventService.eventEmit.emit("updateMsgBadge", { tab2: -1, });
          } else {
            this.eventService.eventEmit.emit("updateMsgBadge", { tab3: -1, });
          }
        }
        const targetObj = { pageId: obj.target, params: {}, sessionId: localStorage.getItem('sessionId') };
        this.appGlobalService.goToForward(targetObj);
      }
    } catch (error) {
      console.log('error----->goPage=', error);
    }
  }
}
